package com.app.bimo.commonbusiness.base;


import android.app.Application;
import android.content.Context;
import android.os.Handler;

import com.app.bimo.commonbusiness.cache.DirManager;
import com.app.bimo.commonbusiness.config.ModuleLifecycleConfig;
import com.app.bimo.commonbusiness.util.BlockDetectByPrinter;
import com.app.bimo.commonbusiness.util.SystemUtil;
import com.baidu.mobstat.StatService;
import com.tencent.bugly.crashreport.CrashReport;

import java.io.File;

import androidx.multidex.MultiDex;



public class BaseApplication extends Application {


    private static Handler mMainThreadHandler;

    private static Context mContext;


    @Override
    public void onCreate() {
        super.onCreate();

        if (mContext == null){
            mContext = getApplicationContext();
        }

        mContext = this;
        DirManager.initDirManager(mContext);
        mMainThreadHandler = new Handler();

        ModuleLifecycleConfig.getInstance().initModuleAhead(this);
        //....
        //初始化组件(靠后)
        ModuleLifecycleConfig.getInstance().initModuleLow(this);
        BlockDetectByPrinter.start();
        StatService.autoTrace(getApplicationContext(), true, false);
//        StatService.start(getApplicationContext());
//        StatService.setDebugOn(true);
//        // 开发时调用，建议上线前关闭，以免影响性能
        initBugLy();

        if (mContext == null){
            mContext = getApplicationContext();
        }

//        LeakCanary.install(this);


    }

    private void initBugLy() {
        Context context = getApplicationContext();
        // 获取当前包名
        String packageName = context.getPackageName();
        // 获取当前进程名
        String processName = SystemUtil.getProcessName(android.os.Process.myPid());
        // 设置是否为上报进程
        CrashReport.UserStrategy strategy = new CrashReport.UserStrategy(context);
        strategy.setUploadProcess(processName == null || processName.equals(packageName));
        // 初始化Bugly
        CrashReport.initCrashReport(context, "2cfebfc537", true, strategy);
        // 如果通过“AndroidManifest.xml”来配置APP信息，初始化方法如下
        // CrashReport.initCrashReport(context, strategy);
    }



    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    public synchronized static Context getContext() {
        return mContext;
    }



    public static Handler getMainThreadHandler() {
        return mMainThreadHandler;
    }
}
